package 代码记录.指定公司代码练习记录.西安.分值100分;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

/**
 * @author zx
 * @create 2023-07-03 22:57
 * 人家的思路很巧妙
 */
public class 身高排序 {//18
    public static void main(String[] args) {//看懂了，挺简单
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();
        int n = sc.nextInt();
        List<Integer> list = new ArrayList<>(n);
        for (int i = 0; i < n; ++i) {
            list.add(sc.nextInt());
        }
        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                int diff1 = Math.abs(o1 - h);
                int diff2 = Math.abs(o2 - h);
                return diff1 == diff2 ? o1 - o2 : diff1 - diff2;//如果差值相等，按照值大小排序，否则差值大小排序
            }
        });
        for(Integer i : list){
            System.out.print(i + " ");
        }
    }
}
